package se.jayway.opengl.tutorial.mesh;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import android.util.Log;

public class Cube extends Mesh {

	public Cube() {
		float[] floatVertices = { -0.5f, -0.5f, 0.5f, 0, 1, 0.5f, -0.5f, 0.5f,
				1, 1, 0.5f, 0.5f, 0.5f, 1, 0, -0.5f, 0.5f, 0.5f, 0, 0,

				0.5f, -0.5f, 0.5f, 0, 1, 0.5f, -0.5f, -0.5f, 1, 1, 0.5f, 0.5f,
				-0.5f, 1, 0, 0.5f, 0.5f, 0.5f, 0, 0,

				0.5f, -0.5f, -0.5f, 0, 1, -0.5f, -0.5f, -0.5f, 1, 1, -0.5f,
				0.5f, -0.5f, 1, 0, 0.5f, 0.5f, -0.5f, 0, 0,

				-0.5f, -0.5f, -0.5f, 0, 1, -0.5f, -0.5f, 0.5f, 1, 1, -0.5f,
				0.5f, 0.5f, 1, 0, -0.5f, 0.5f, -0.5f, 0, 0,

				-0.5f, 0.5f, 0.5f, 0, 1, 0.5f, 0.5f, 0.5f, 1, 1, 0.5f, 0.5f,
				-0.5f, 1, 0, -0.5f, 0.5f, -0.5f, 0, 0,

				-0.5f, -0.5f, 0.5f, 0, 1, 0.5f, -0.5f, 0.5f, 1, 1, 0.5f, -0.5f,
				-0.5f, 1, 0, -0.5f, -0.5f, -0.5f, 0, 0 };

		short[] shortIndices = { 0, 1, 3, 1, 2, 3, 4, 5, 7, 5, 6, 7, 8, 9, 11,
				9, 10, 11, 12, 13, 15, 13, 14, 15, 16, 17, 19, 17, 18, 19, 20,
				21, 23, 21, 22, 23, };
		Log.i("Test", "" + shortIndices.length);
		int maxVertices = 24;
		int maxIndices = 36;

		hasColor = false;
		hasTexCoords = true;
		vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4;
		tmpBuffer = new int[maxVertices * vertexSize / 4];

		ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
		buffer.order(ByteOrder.nativeOrder());
		vertices = buffer.asIntBuffer();

		if (maxIndices > 0) {
			buffer = ByteBuffer.allocateDirect(maxIndices * Short.SIZE / 8);
			buffer.order(ByteOrder.nativeOrder());
			indices = buffer.asShortBuffer();
		} else {
			indices = null;
		}

		setVertices(floatVertices);
		setIndices(shortIndices);
	}

	public Cube(float width, float height, float depth, int widthSegments,
			int heightSegments, int depthSegments) {

	}

	public Cube(float width, float height, float depth) {
		width /= 2;
		height /= 2;
		depth /= 2;
		// Mapping coordinates for the vertices
		float textureCoordinates[] = { 0.0f, 1.0f, //
				1.0f, 1.0f, //
				0.0f, 0.0f, //
				1.0f, 0.0f, //
				0.0f, 1.0f, //
				1.0f, 1.0f, //
				0.0f, 0.0f, //
				1.0f, 0.0f, //
				0.0f, 1.0f, //
				1.0f, 1.0f, //
				0.0f, 0.0f, //
				1.0f, 0.0f, //
				0.0f, 1.0f, //
				1.0f, 1.0f, //
				0.0f, 0.0f, //
				1.0f, 0.0f, //
				0.0f, 1.0f, //
				1.0f, 1.0f, //
				0.0f, 0.0f, //
				1.0f, 0.0f, //
				0.0f, 1.0f, //
				1.0f, 1.0f, //
				0.0f, 0.0f, //
				1.0f, 0.0f };

		float vertices[] = { -width, -height, -depth, // 0
				width, -height, -depth, // 1
				width, height, -depth, // 2
				-width, height, -depth, // 3
				-width, -height, depth, // 4
				width, -height, depth, // 5
				width, height, depth, // 6
				-width, height, depth, // 7
		};

		short indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7,
				3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5, 3, 0, 1, 3, 1, 2, };

		setIndices(indices);
		setVertices(vertices);
		setTextureCoordinates(textureCoordinates);
	}
}
